package rd.hardcore.engine.sample.process {
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	import rd.hardcore.engine.sample.EncodingData;
	import rd.hardcore.engine.sample.SampleDatas;

	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.NativeProcessExitEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.system.Capabilities;

	/**
	 * @author plepers
	 */
	public class MP3Encoder extends EventDispatcher{
		
		private static const MAC_EXEC : File = new File( "app:/nplib/lame/lame");
		private static const WIN_EXEC : File = new File( "app:/nplib/lame/lame.exe" );

		
		public function MP3Encoder ( sampleData : SampleDatas, encodingData : EncodingData ) {
			
			_sampleData = sampleData;
			_encodingData = encodingData;
			
		}

		private function exec () : void {
			_buildProccess( );
		}

		private function _buildProccess () : void {
			
			var lameArgs : LameArguments = new LameArguments( );
			lameArgs.bitrate = _encodingData.bitrate;
			
			var _npsi : NativeProcessStartupInfo = new NativeProcessStartupInfo( );
			_npsi.arguments = lameArgs.getArguments( );
			_npsi.executable = getOsExecutable( );
			
			_output = new ByteArray();
			
			_process = new NativeProcess( );
			_process.start( _npsi );
			registerProcess();
			
			_sampleData.writeRange( _process.standardInput );
			
		}

		private function getOsExecutable () : File {
			
			
			if( Capabilities.os.toLowerCase().indexOf( "mac" ) == 0 ) 
				return MAC_EXEC;
			else if ( Capabilities.os.toLowerCase().indexOf( "win" ) == 0 )
				return WIN_EXEC;
			else
				throw new Error( "rd.hardcore.engine.sample.process.MP3Encoder - getOsExecutable : Lame executable not found for "+Capabilities.os );
			
			return null;
		}
		
		private function registerProcess() : void {
			
			_process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
            _process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
            _process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
            _process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
            _process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
            _process.addEventListener(Event.ACTIVATE, onActivate);
			
		}
		
		private function onActivate(event:Event):void
        {
            trace("MxmlcProcess---> "+event);
        }
        private function onOutputData(event:ProgressEvent):void
        {
			
			var p : NativeProcess = event.currentTarget as NativeProcess;
			trace("MxmlcProcess---> Got: ", p.standardOutput.readUTFBytes(p.standardOutput.bytesAvailable)); 

        }
        
        private function onErrorData(event:ProgressEvent):void
        {
        	var p : NativeProcess = event.currentTarget as NativeProcess;
            trace("MP3Encoder---> ERROR -", p.standardError.readUTFBytes(p.standardError.bytesAvailable));
             
			p.removeEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
            p.removeEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
            p.removeEventListener(NativeProcessExitEvent.EXIT, onExit);
            p.removeEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
            p.removeEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
            p.removeEventListener(Event.ACTIVATE, onActivate);
        }
        
        private function onExit(event:NativeProcessExitEvent):void
        {
			
			var p : NativeProcess = event.currentTarget as NativeProcess;
            trace("MP3Encoder---> Process exited with ", event.exitCode);
        }
        
        private function onIOError(event:IOErrorEvent):void
        {
             trace("MP3Encoder--->"+ event.toString());
        }

		
		private var _sampleData : SampleDatas;

		private var _encodingData : EncodingData;
		
		private var _process : NativeProcess;
		
		private var _output : ByteArray;
		
	}
}
